/*
* Poppy Game Engine - https://pgetemplate.googlecode.com/svn/
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* Copyright (c) 2008 Poppy
*/

#include "stdafx.h"
#include "CMemStream.h"

namespace PGESystem
{

	CMemStream::CMemStream( void* data, int size ) : CBinaryStream()
	{
		m_pData = data;
		m_dLength = size;
	}

	CMemStream::CMemStream() : CBinaryStream()
	{
		m_pData = 0;
	}

	CMemStream::~CMemStream()
	{

	}

	int CMemStream::GetLength()
	{
		return m_dLength;
	}

	int CMemStream::BinaryRead(void * buffer, unsigned int size)
	{
		unsigned int i(0);
		for( ; (i < size) && (m_dFilePointer < m_dLength); i++ )
		{
			((char*)buffer)[i] = ((char*)m_pData)[m_dFilePointer++];
		}
		return i;
	}

	int CMemStream::BinaryWrite(void * buffer, unsigned int size)
	{
		unsigned int i(0);
		for( ; (i < size) && (m_dFilePointer < m_dLength); i++ )
		{
			((char*)m_pData)[m_dFilePointer++] = ((char*)buffer)[i];
		}
		return i;
	}

	int CMemStream::Read(char * buffer)
	{
		int i(0);
		for( ; m_dFilePointer < m_dLength; i++ )
		{
			((char*)buffer)[i] = ((char*)m_pData)[m_dFilePointer++];
			if( ((char*)buffer)[i] == 0 ) return i + 1;
		}
		((char*)buffer)[i] = 0;
		return i;
	}

	int CMemStream::Write(char * buffer)
	{
		int i(0);
		for( ; m_dFilePointer < m_dLength; i++ )
		{
			((char*)m_pData)[m_dFilePointer++] = ((char*)buffer)[i];
			if( ((char*)buffer)[i] == 0 ) return i + 1;
		}
		return i;
	}

}
